package com.example.demo.struct.flyweight;

import java.awt.Color;
import java.util.HashMap;

/**
 * @Desc:
 * @Author zzz
 * @Date 2021/2/24
 * @Version V1.0
 */
public class WuziqiFactory {

    private HashMap<Color, Wuziqi> cache = new HashMap<>();

    public Wuziqi getQizi(Color color) {
        Wuziqi wuziqi = null;
        if (Color.WHITE.equals(color)) {
            wuziqi = cache.get(color);
            if (wuziqi != null) {
                return wuziqi;
            } else {
                wuziqi = new White();
                cache.put(color, wuziqi);
            }
        } else if (Color.BLACK.equals(color)) {
            wuziqi = cache.get(color);
            if (wuziqi != null) {
                return wuziqi;
            } else {
                wuziqi = new Black();
                cache.put(color, wuziqi);
            }
        }
        return wuziqi;
    }

}
